package org.example.comment.controller;

import org.example.comment.entity.Comment;
import org.example.comment.entity.vo.CommentVO;
import org.example.entity.Base.Result;
import org.example.comment.service.CommentService;
import org.example.util.JWTUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import jakarta.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/comment")
@Validated
public class CommentController {
    private final CommentService commentService;

    public CommentController(CommentService commentService) {
        this.commentService = commentService;
    }

    @PostMapping("/addComment")
    public Result<Comment> addComment(HttpServletRequest request, Integer
        CommentId, String CommentText, Integer TipId, Integer FromCommentId){
        String CommentTime=(LocalDateTime.now()).toString();
        //获取令牌
        String token=request.getHeader("Authorization");
        Map<String, Object> c= JWTUtil.parseToken(token);
        String UserEmail=(String)(c.get("Email"));
        System.out.println(CommentText);
        Comment comment=new Comment(CommentId,CommentText,CommentTime,UserEmail,TipId,FromCommentId);
        commentService.addComment(comment);
        return Result.success(comment);
    }
     @GetMapping("/getComment")
     public Result<List<CommentVO>> getComment(Integer TipId){
        System.out.println(TipId);
        List<CommentVO> comments=commentService.getComment(TipId);
        return Result.success(comments);
    }

}
